<?php
/**
 * 提供操作帮助
 */

function help(){
    echo "\n";
    echo "     欢迎使用保护伞公司图书管理系统\n";
    echo "\n";
    echo "     ----- 如需开卡请输入 1 -----\n";
    echo "     ----- 如需借书请输入 2 -----\n";
    echo "     ----- 如需还书请输入 3 -----\n";
    echo "     ----- 录入书籍请输入 4 -----\n";
    echo "\n";
}
/**
 * 开卡流程
 */
function makeCard()
{
    echo "成功进入开卡流程    \n";
    $dsn = 'mysql:dbname=Library;host=localhost';
    $user = 'root';
    $password = '123456';
    try {
        do {
            $db = new PDO($dsn, $user, $password);

            echo "请输入用户名：";
            $user_name = trim(fgets(STDIN));
            echo "请输入手机号码作为用户借书卡号：";
            $user_phone = trim(fgets(STDIN));
            echo "请输入密码：";
            $user_password = trim(fgets(STDIN));

            //检索手机号码（借书卡号）是否唯一
            $statement = $db->query("SELECT COUNT(PHONE) FROM Users WHERE phone=$user_phone");
            $count = (int)$statement->fetch()[0];

            while ($count == 1) {
                echo "此卡号已存在！";
                echo "请重新输入手机号码：";
                $user_phone = trim(fgets(STDIN));
                $statement = $db->query("SELECT COUNT(PHONE) FROM Users WHERE phone=$user_phone");
                $count = (int)$statement->fetch()[0];
            }

            echo "请确认您的开卡信息\n";
            echo "-------------------------\n";
            echo "\n";
            echo "用户名：" . $user_name . "\n";
            echo "卡号：" . $user_phone . "\n";
            echo "密码：" . $user_password . "\n";
            echo "\n";
            echo "-------------------------\n";
            echo "输入 y 确认，任意键重新输入：";

            $confirm = trim(fgets(STDIN));
            $confirm = strtolower($confirm);

        } while ($confirm != 'y');

        $db->exec("INSERT INTO Users SET user ='$user_name' ,phone ='$user_phone' ,password ='$user_password';");
        echo "恭喜您开卡成功！" . "\n";

    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
        echo "\n";
    }


}

/**
 * 借书流程
 */
function makeBorrow()
{
    echo "成功进入借书功能    \n";
    $dsn = 'mysql:dbname=Library;host=localhost';
    $user = 'root';
    $password = '123456';

    try{
        echo "请输入借书人的卡号（即手机号码）：";
        $user_phone = trim(fgets(STDIN));

        do{
        $db = new PDO($dsn,$user,$password);

            echo "请输入需要借书的书籍编号：";
            $book_number = trim(fgets(STDIN));
            $date = date("y-m-d");
            echo "借书时间为 ".$date."\n";
            echo "请于 ".date('y-m-d',strtotime('+30 day'))." 之前归还"."\n";

            //写入借书数据并检测借书的总数
            $db->exec("INSERT INTO Record SET phone = '$user_phone' ,number = '$book_number',date = '$date'");
            $statement=$db->query("SELECT COUNT(phone) AS amount FROM Record WHERE phone=$user_phone");
            $count = (int)$statement->fetch()[0];
            echo "您目前已经借了".$count."本书还未归还"."\n";

            echo "若要继续借书请输入 y ，任意键返回 ";
            $confirm = trim(fgets(STDIN));
            $confirm = strtolower($confirm);

        } while ($confirm == 'y');

    }catch(PDOException $e){
        echo'Connection failed: '.$e->getMessage();
        echo "\n";
    }

}

/**
 * 还书流程
 */
function  makeReturn()
{
    echo "成功进入还书功能    \n";
    $dsn = 'mysql:dbname=Library;host=localhost';
    $user = 'root';
    $password = '123456';
    try{
        do{
            $db = new PDO($dsn,$user,$password);

            echo "请输入需要还书的书籍编号：";
            $book_number = trim(fgets(STDIN));

            $statement=$db->query("SELECT date FROM Record WHERE Number=$book_number");
            $date_borrow = (string)$statement->fetch()[0];
            echo "您的借书时间为 ".$date_borrow."\n";

            //借书还书的时间差
            $date_borrow = "$date_borrow";
            $borrow = strtotime("$date_borrow");
            $date_back = date("y-m-d");//还书的时间
            $return = strtotime("$date_back");
            $day = ($return - $borrow)/86400;
            echo "还书时间为 ".$date_back ."\n";
            echo "您一共借了 ".$day." 天\n";

            if($day>30){
                echo "根据图书馆规定，每超过一天需收取一元钱的费用\n";
                echo "您一共需缴纳".($day-30)."元\n";
                echo "完成缴费请输入 y ,任意键退出 ";
                $pay = trim(fgets(STDIN));
                $pay = strtolower($pay);
                if($pay == 'y'){

                }else{
                    echo "还书失败！(未完成缴费)\n";
                    return;
                }
            }

            $db->exec("UPDATE Record SET dateback='$date_back' WHERE number=$book_number");
            echo "恭喜您还书成功！\n";

            echo "若要继续还书请输入 y ，任意键返回 ";
            $confirm = trim(fgets(STDIN));
            $confirm = strtolower($confirm);

        } while ($confirm == 'y');

    }catch(PDOException $e){
        echo'Connection failed: '.$e->getMessage();
        echo "\n";
    }
}

/**
 * 录入书籍
 */
function  entryBooks()
{
    echo "成功进入书籍录入功能    \n";
    $dsn = 'mysql:dbname=Library;host=localhost';
    $user = 'root';
    $password = '123456';
    try{
        do{
            $db = new PDO($dsn,$user,$password);

            echo "书籍名称：";
            $book = trim(fgets(STDIN));
            echo "书籍编号：";
            $book_number = trim(fgets(STDIN));
            echo "书籍作者：";
            $book_auther = trim(fgets(STDIN));
            echo "出版社：";
            $book_press = trim(fgets(STDIN));
            echo "书籍价格：";
            $book_value = trim(fgets(STDIN));

            //检索书籍编号是否唯一
            $statement=$db->query("SELECT COUNT(number) FROM Books WHERE number=$book_number");
            $count = (int)$statement->fetch()[0];

            while ($count == 1) {
                echo "此书籍编号已存在！";
                echo "请重新录入书籍编号：";
                $book_number = trim(fgets(STDIN));
                $statement=$db->query("SELECT COUNT(number) FROM Books WHERE number=$book_number");
                $count = (int)$statement->fetch()[0];
            }
            echo "书籍录入信息如下：\n";
            echo "--------------------------\n";
            echo "书籍名称：".$book."\n";
            echo "书籍编号：".$book_number."\n";
            echo "书籍作者：".$book_auther."\n";
            echo "出版社：".$book_press."\n";
            echo "书籍价格：".$book_value."\n";
            echo "--------------------------\n";

            $db->exec("INSERT INTO Books SET book='$book',number ='$book_number',auther='$book_auther',press='$book_press',value='$book_value'");
            echo "书籍录入成功！\n";

            echo "若要继续录入书籍请输入 y ，任意键返回 ";
            $confirm = trim(fgets(STDIN));
            $confirm = strtolower($confirm);

        } while ($confirm == 'y');

    }catch(PDOException $e){
        echo'Connection failed: '.$e->getMessage();
        echo "\n";
    }
}



/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/22
 * Time: 18:55
 */



